﻿using MyInspiration.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Taoke.filter;


namespace Taoke.Controllers
{
    public class DataController : Controller
    {
        //
        // GET: /Data/
        /// <summary>
        /// 录入
        /// </summary>
        /// <returns></returns>
        /// 
        [cors]
        public ActionResult Entering()
        {
            //接收数据
            string arr = Request.Form["arrData[]"];
            if (!string.IsNullOrEmpty(arr))
            {
                List<string> list = arr.Split(',').ToList();
                return EnteringData(list);
            }
            else
            {
                return Content(JsonConvert.SerializeObject(new
                {
                    status = "fail",
                    msg = "受影响行数为:" + 0
                }, Formatting.Indented), "application/json");
            }


        }
        public ContentResult EnteringData(List<string> list)
        {
            list = list.Distinct().ToList();
            //录入数据
            using (MyIdeaEntities tk = new MyIdeaEntities())
            {
                foreach (var item in list)
                {
                    tk.goodsInfo.Add(new goodsInfo()
                    {
                        goodid = item,
                        status = false,
                        entryTime = DateTime.Now
                    });
                }
                int flag = tk.SaveChanges();
                if (flag > 0)
                {
                    return Content(JsonConvert.SerializeObject(new
                    {
                        status = "success",
                        msg = "受影响行数为:" + flag
                    }, Formatting.Indented), "application/json");
                }
                else
                {
                    return Content(JsonConvert.SerializeObject(new
                    {
                        status = "fail",
                        msg = "受影响行数为:" + 0
                    }, Formatting.Indented), "application/json");
                }
            }



        }
        /// <summary>
        /// 得到商品信息列表
        /// </summary>
        /// <returns>json</returns>
        /// 
        [cors]
        public ActionResult GetGoodsInfoList()
        {
            using (MyIdeaEntities tk = new MyIdeaEntities())
            {
                var list = tk.goodsInfo.Where(m => m.status == false).Select(m => new
                {
                    id = m.goodid,
                    entryTime = m.entryTime
                });
                return Content(JsonConvert.SerializeObject(list, Formatting.Indented), "application/json");
            }

        }
        /// <summary>
        /// 添加转码后的数据
        /// </summary>
        /// <param name="after"></param>
        /// <returns></returns>
        /// 
        [cors]
        [ValidateInput(false)]
        public ActionResult AddTranscodingData(afterTranscoding after)
        {
            using (MyIdeaEntities tk = new MyIdeaEntities())
            {
                after.entryTime = DateTime.Now;
                tk.afterTranscoding.Add(after);
                var list = tk.goodsInfo.Where(m => m.goodid == after.goodId);
                foreach (var item in list)
                {
                    item.status = true;
                }
                int flag = tk.SaveChanges();
                if (flag > 0)
                {
                    return Content(JsonConvert.SerializeObject(new
                    {
                        status = "success",
                        msg = "受影响行数为:" + flag
                    }, Formatting.Indented), "application/json");
                }
                else
                {
                    return Content(JsonConvert.SerializeObject(new
                    {
                        status = "fail",
                        msg = "受影响行数为:" + 0
                    }, Formatting.Indented), "application/json");
                }
            }

        }
        /// <summary>
        /// 得到转码后的商品数据
        /// </summary>
        /// <returns>json</returns>
        /// 
        [cors]
        public ActionResult GetAfterTranscodingDataList(int pageIndex = 1, int pageSize = 80, string goodContent = "")
        {
            using (MyIdeaEntities tk = new MyIdeaEntities())
            {
                if (string.IsNullOrEmpty(goodContent))
                {
                    var tempDate = DateTime.Now.AddDays(-3);
                    var list = tk.afterTranscoding.Select(m => new
                    {
                        Content = m.Content,
                        entryTime = m.entryTime
                    }).Where(m => m.entryTime > tempDate);

                    int zys/*总页数*/ = (list.Count() + pageSize - 1) / pageSize;
                    var show_data/*json数据*/ = new { data = list.OrderByDescending(m => m.entryTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize), count = zys };
                    return Content(JsonConvert.SerializeObject(show_data, Formatting.Indented), "application/json");
                }
                else
                {
                    var list = tk.afterTranscoding.Select(m => new
                    {
                        Content = m.Content,
                        entryTime = m.entryTime
                    }).Where(m => m.Content.Contains(goodContent));

                    int zys/*总页数*/ = (list.Count() + pageSize - 1) / pageSize;
                    var show_data/*json数据*/ = new { data = list.OrderByDescending(m => m.entryTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize), count = zys };
                    return Content(JsonConvert.SerializeObject(show_data, Formatting.Indented), "application/json");
                }

            }
        }
    }
}
